import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 导入路由组件
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import PropertyList from '@/views/RoomManage/PropertyList.vue'
import PropertyPublish from '@/views/RoomManage/PropertyPublish.vue'
import RoomHot from '@/views/RoomHot/index.vue'
import OrderManage from '@/views/OrderManage/index.vue'
import EvaluateManage from '@/views/EvaluateManage/index.vue'
import UserInfo from '@/views/UserCenter/UserInfo.vue'
import UserPassword from '@/views/UserCenter/UserPassword.vue'

import Login from '@/views/Login/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: Login
    }, //登录页
    {
      path: '/',
      component: Layout,
      children: [
        { path: '', component: Home }, //Home页
        { path: '/property/list', component: PropertyList }, //房源列表页
        { path: '/property/publish', component: PropertyPublish }, //发布房源页
        { path: '/hot', component: RoomHot }, //热门房源页
        { path: '/order', component: OrderManage }, //预约管理页
        { path: '/evaluate', component: EvaluateManage }, //评价管理页
        { path: '/user/info', component: UserInfo }, //基本资料页
        { path: '/user/password', component: UserPassword } //重置密码页
      ]
    } //布局架子
  ]
})

// 路由导航守卫，没有登录时访问拦截
router.beforeEach((to) => {
  const UserStore = useUserStore()
  if (!UserStore.UserList.token && to.path !== '/login') {
    return '/login' //重定向到登录页面
  }
})

export default router
